package com.juc.interrupt;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Author Zan
 * @Create 2024/5/8 16:30
 * @ClassName : InterruptDemo
 * @Description : 面试题：当前线程的中断标识为true，是不是就立刻停止？
 */
public class InterruptDemo2 {

    public static void main(String[] args) {
        // 实例方法interrupt()仅仅是设置线程的中断状态设置为true，不会停止线程
        Thread t1 = new Thread(() -> {
            for (int i = 1; i <= 300; i++) {
                System.out.println("-----" + i);
            }
            System.out.println("t1线程调用interrupt()后的中断标志02：" + Thread.currentThread().isInterrupted()); // true
        }, "t1");
        t1.start();

        System.out.println("t1线程默认的中断标志：" + t1.isInterrupted()); // false

        try {
            TimeUnit.MILLISECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        t1.interrupt(); // true
        System.out.println("t1线程调用interrupt()后的中断标志01：" + t1.isInterrupted()); // true

        try {
            TimeUnit.MILLISECONDS.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // 线程已经结束了
        System.out.println("t1线程调用interrupt()后的中断标志03：" + t1.isInterrupted()); // ???? - false 不活动的线程不会产生任何影响
    }
}
